package top.mataiji.manager.controller;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.mataiji.manager.pojo.vo.BookVo;
import top.mataiji.manager.service.BookImgService;
import top.mataiji.manager.util.Code;
import top.mataiji.manager.util.Common;
import top.mataiji.manager.util.Result;
import top.mataiji.manager.pojo.Book;
import top.mataiji.manager.pojo.Cart;
import top.mataiji.manager.pojo.User;
import top.mataiji.manager.pojo.vo.CartVo;
import top.mataiji.manager.service.BookService;
import top.mataiji.manager.service.UserService;
import top.mataiji.manager.service.CartService;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Auther: zhaoss
 * @Date: 2021/3/14 - 03 - 14 - 22:13
 * @Description: top.mataiji.manager.controller
 * @version: 1.0
 */
@RestController
//@RequestMapping("manager")
public class ManagerController {

    @Autowired
    UserService userService;
    @Autowired
    BookService bookService;
    @Autowired
    CartService cartService;
    @Autowired
    BookImgService bookImgService;

    /**
     * 登录页面
     *
     * @param loginname
     * @param password
     * @return
     */
    @PostMapping("login")
    public Result login(String loginname, String password) {
        //创建查询对象
        User user = new User();
        //设置查询参数
        user.setLoginname(loginname);
        user.setPassword(password);
        //登录查询验证
        User login = userService.login(user);
        if (login != null) {
//            Result result = new Result();
            return Result.success("登录成功", login);
        } else {
            return Result.error("登录失败");
        }
    }

    @RequestMapping("getCode")
    public String getCode(HttpSession session) {
        session.removeAttribute("code");
        String code = Code.getCodes();
        session.setAttribute("code", code);
        return code;
    }

    @RequestMapping("findAllBook")
    public List<Book> findAllBook() {
        return bookService.findAll();
    }

    @RequestMapping("findBookById")
    public BookVo findBookById(Integer bookId) {
        Book bookById = bookService.findBookById(bookId);
        BookVo bookVo = new BookVo();
        bookVo.setBook(bookById);
        bookVo.setBookimg(bookImgService.findAllById(bookId));
        return bookVo;
    }

    @RequestMapping("deleteBookById")
    public String deleteBookById(Integer bookId) {
        int i = bookService.deleteBookById(bookId);
        if (i > 0) {
            return "success";
        }
        return "error";
    }

    @RequestMapping("selectAllBook")
    public PageInfo<Book> selectAllBook(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize") Integer pagaSize) {
        return bookService.findAllBypage(pageNum, pagaSize);
    }

    @RequestMapping("addCart")
    public String addCart(Integer userId, Integer bookId, Integer bookNum) {
        System.out.println(bookId + " " + bookNum + " " + userId);
        int falg = 0;
        //查询数据库中是否有该条记录
        Cart i = cartService.selectById(userId, bookId);
        if (i != null) {
            //改变商品数据
            i.setBookNum(i.getBookNum() + bookNum);
            falg = cartService.updateBook(i);
        } else {
            //在购物车里添加新书籍
            i = new Cart();
            i.setCartId(Common.getOrderId() + userId);
            i.setUserId(userId);
            i.setBookNum(bookNum.longValue());
            i.setBookId(bookId);
            falg = cartService.addBook(i);
        }
        if (falg == 0) {
            return "error";
        } else {
            return "success";
        }
    }

    @RequestMapping("cart/findAllUserId")
    public List<CartVo> findAllUserId(Integer userId) {
        return cartService.findAllUserId(userId);
    }
}
